
import authentication.Role
import authentication.User
import authentication.UserRole
import librarymanager.Author
import librarymanager.Book
import librarymanager.Category

class BootStrap {

    def init = { servletContext ->
        Role rStudent;
        Role rProfessor;
        Role rManager;
        Role rAdmin;
        if (!User.count()) {
            // creating roles
            rStudent = new Role(authority: 'ROLE_STUDENT').save(flush: true)
            rProfessor = new Role(authority: 'ROLE_PROFESSOR').save(flush: true)
            rManager = new Role(authority: 'ROLE_MANAGER').save(flush: true)
            rAdmin = new Role(authority: 'ROLE_ADMIN').save(flush: true)
            
            // creating admin user
            User user = new User(username: 'admin', password: 'somepasswordhere').save(flush: true)
            new UserRole(user: user, role: rAdmin).save(flush: true)
            
            user = new User(username: 'manager', password: 'manager').save(flush: true)
            new UserRole(user: user, role: rManager).save(flush: true)
            
            user = new User(username: 'professor', password: 'professor').save(flush: true)
            new UserRole(user: user, role: rProfessor).save(flush: true)
            
            user = new User(username: 'student', password: 'student').save(flush: true)
            new UserRole(user: user, role: rStudent).save(flush: true)
            
            if (!Book.count()) {
                Author a;
                Category c1 = new Category(name: "Public", roles: [rAdmin, rManager, rProfessor, rStudent]).save(flush: true)
                Category c2 = new Category(name: "Protected", roles: [rAdmin, rManager, rProfessor]).save(flush: true)
                Category c3 = new Category(name: "Private", roles: [rAdmin, rManager]).save(flush: true)
                
                for (int i = 1; i <= 10; i++) {
                    a = new Author(name: "Author${i}").save(flush: true)
                    if (i%3) {
                        new Book(title:"Book A${i}", category: c1, author: a).save(flush: true)
                        new Book(title:"Book B${i}", category: c2, author: a).save(flush: true)
                        new Book(title:"Book C${i}", category: c3, author: a).save(flush: true)
                    } else if (i%2) {
                        new Book(title:"Book A${i}", category: c3, author: a).save(flush: true)
                        new Book(title:"Book B${i}", category: c1, author: a).save(flush: true)
                        new Book(title:"Book C${i}", category: c2, author: a).save(flush: true)
                    } else {
                        new Book(title:"Book A${i}", category: c2, author: a).save(flush: true)
                        new Book(title:"Book B${i}", category: c3, author: a).save(flush: true)
                        new Book(title:"Book C${i}", category: c1, author: a).save(flush: true)
                    }
                    
                }
            }
        }
    }
        
      
    def destroy = {
    }
}
